package com.example.sys.entity.types;

import org.apache.ibatis.type.*;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;



/**
 * @author ZhengDing
 * @date 2022/1/28 10:21
 */
@MappedJdbcTypes(JdbcType.TINYINT)
@MappedTypes(String.class)
public class ArticleTypeHandle extends BaseTypeHandler<String> {

    private static final Map<Integer, String> DICT = new HashMap<Integer, String>();
    private static final Map<String, Integer> DICT2 = new HashMap<String, Integer>();
    static {
        DICT.put(0, "Java");
        DICT.put(1, "Python");

        DICT2.put("Java", 0);
        DICT2.put("Python", 1);
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, DICT2.get(s));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return DICT.get(resultSet.getInt(s));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return DICT.get(resultSet.getInt(i));
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return DICT.get(callableStatement.getInt(i));
    }
}
